<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"/>
		<title>Alias UniquePtr</title>
		<link rel="stylesheet" type="text/css" href="../../styles/ddox.css"/>
		<link rel="stylesheet" href="../../prettify/prettify.css" type="text/css"/>
		<script type="text/javascript" src="../../scripts/jquery.js">/**/</script><script type="text/javascript" src="../../scripts/ddox.js">/**/</script>
	</head>
	<body onload="setupDdox();">
		<nav id="main-nav">
			<noscript>
				<p style="color: red">The search functionality needs JavaScript enabled</p>
			</noscript>
			<div id="symbolSearchPane" style="display: none">
				<form action="#" method="GET">
					<input id="symbolSearch" type="text" name="q" placeholder="Search for symbols" autocomplete="off" onchange="performSymbolSearch(40);" onkeypress="this.onchange();" onpaste="this.onchange();" oninput="this.onchange();"/>
				</form>
				<ul id="symbolSearchResults" class="symbolList" style="display: none"></ul><script type="application/javascript" src="../../symbols.js"></script><script type="application/javascript">var symbolSearchRootDir = "../../";
$('#symbolSearchPane').show();</script>
			</div>
			<ul class="tree-view">
				<li class="tree-view ">
					<div class="package ">autoptr
					</div>
			<ul class="tree-view">
				<li>
					<div class="module ">
						<a href="../../autoptr/common.html">common</a>
					</div>
				</li>
				<li>
					<div class="module ">
						<a href="../../autoptr/intrusive_ptr.html">intrusive_ptr</a>
					</div>
				</li>
				<li>
					<div class="module ">
						<a href="../../autoptr/rc_ptr.html">rc_ptr</a>
					</div>
				</li>
				<li>
					<div class="module ">
						<a href="../../autoptr/shared_ptr.html">shared_ptr</a>
					</div>
				</li>
				<li>
					<div class="module selected">
						<a href="../../autoptr/unique_ptr.html">unique_ptr</a>
					</div>
				</li>
			</ul>
				</li>
			</ul>
		</nav>
		<div id="main-contents">
			<h1>Alias UniquePtr</h1><p><code class="lang-d"><span class="typ">UniquePtr</span></code> is a smart pointer that owns and manages object through a pointer and disposes of that object when the <code class="lang-d"><span class="typ">UniquePtr</span></code> goes out of scope.
</p>
			<div class="prototype">
				<code class="lang-d">
					<div class="single-prototype">
			<span class="kwd">alias</span> <span class="pln">UniquePtr</span>(_Type, _DestructorType, _ControlType)
			 <span class="pun">=</span> <a href="../../autoptr/rc_ptr/RcPtr.html"><span class="typ">RcPtr</span></a>!(_Type,_DestructorType,_ControlType)<span class="pun">;</span>
					</div>
					<div class="single-prototype">
			<span class="kwd">alias</span> <span class="pln">UniquePtr</span>(_Type, _ControlType, _DestructorType)
			 <span class="pun">=</span> <a href="../../autoptr/rc_ptr/RcPtr.html"><span class="typ">RcPtr</span></a>!(_Type,_DestructorType,_ControlType)<span class="pun">;</span>
					</div>
				</code>
			</div>
			<section><p><code class="lang-d"><span class="typ">UniquePtr</span></code> is alias to <code class="lang-d"><a href="../../autoptr/rc_ptr/RcPtr.html" title="autoptr.rc_ptr.RcPtr"><span class="typ">RcPtr</span></a></code> with immutable <code class="lang-d"><span class="pln">_ControlType</span></code>.
</p>

<p>    The object is destroyed and its memory deallocated when either of the following happens:
</p>

<p>        1. the managing <code class="lang-d"><span class="typ">UniquePtr</span></code> object is destroyed
</p>

<p>        2. the managing <code class="lang-d"><span class="typ">UniquePtr</span></code> object is assigned another pointer via various methods like <code class="lang-d"><span class="pln">opAssign</span></code> and <code class="lang-d"><span class="pln">store</span></code>.
</p>

<p>    The object is destroyed using delete-expression or a custom deleter that is supplied to <code class="lang-d"><span class="typ">UniquePtr</span></code> during construction.
</p>

<p>    A <code class="lang-d"><span class="typ">UniquePtr</span></code> may alternatively own no object, in which case it is called empty.
</p>

<p>    Template parameters:
</p>

<p>        <code class="lang-d"><span class="pln">_Type</span></code> type of managed object
</p>

<p>        <code class="lang-d"><span class="pln">_DestructorType</span></code> function pointer with attributes of destructor, to get attributes of destructor from type use <code class="lang-d"><a href="../../autoptr/common/DestructorType.html" title="autoptr.common.DestructorType"><span class="typ">DestructorType</span></a><span class="pun">!</span><span class="pln">T</span></code>. Destructor of type <code class="lang-d"><span class="pln">_Type</span></code> must be compatible with <code class="lang-d"><span class="pln">_DestructorType</span></code>
</p>

<p>        <code class="lang-d"><span class="pln">_ControlType</span></code> represent type of counter, must by of type immutable <code class="lang-d"><a href="../../autoptr/common/ControlBlock.html" title="autoptr.common.ControlBlock"><span class="typ">ControlBlock</span></a></code>.
</p>
</section>

			<section><h2>Example</h2>

<pre class="code"><code class="lang-d"><span class="pln">
</span><span class="kwd">static class </span><span class="typ">Foo</span><span class="pun">{
    </span><span class="typ">int </span><span class="pln">i</span><span class="pun">;

    </span><span class="kwd">this</span><span class="pun">(</span><span class="typ">int </span><span class="pln">i</span><span class="pun">)</span><span class="kwd">pure nothrow @safe @nogc</span><span class="pun">{
        </span><span class="kwd">this<wbr/></span><span class="pun">.</span><span class="pln">i </span><span class="pun">= </span><span class="pln">i</span><span class="pun">;
    }
}

</span><span class="kwd">static class </span><span class="typ">Bar </span><span class="pun">: </span><span class="typ">Foo</span><span class="pun">{
    </span><span class="typ">double </span><span class="pln">d</span><span class="pun">;

    </span><span class="kwd">this</span><span class="pun">(</span><span class="typ">int </span><span class="pln">i</span><span class="pun">, </span><span class="typ">double </span><span class="pln">d</span><span class="pun">)</span><span class="kwd">pure nothrow @safe @nogc</span><span class="pun">{
        </span><span class="kwd">super</span><span class="pun">(</span><span class="pln">i</span><span class="pun">);
        </span><span class="kwd">this<wbr/></span><span class="pun">.</span><span class="pln">d </span><span class="pun">= </span><span class="pln">d</span><span class="pun">;
    }
}

</span><span class="com">//implicit qualifier cast
</span><span class="pun">{
    </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="kwd">const </span><span class="typ">Foo</span><span class="pun">) </span><span class="pln">foo </span><span class="pun">=  </span><span class="typ">UniquePtr</span><span class="pun">!</span><span class="typ">Foo<wbr/></span><span class="pun">.</span><span class="pln">make</span><span class="pun">(</span><span class="lit">42</span><span class="pun">);
    </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">foo<wbr/></span><span class="pun">.</span><span class="pln">get<wbr/></span><span class="pun">.</span><span class="pln">i </span><span class="pun">== </span><span class="lit">42</span><span class="pun">);

    </span><span class="kwd">const </span><span class="typ">UniquePtr</span><span class="pun">!</span><span class="typ">Foo </span><span class="pln">foo2 </span><span class="pun">= </span><span class="pln">foo<wbr/></span><span class="pun">.</span><span class="pln">move</span><span class="pun">;
    </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">foo2<wbr/></span><span class="pun">.</span><span class="pln">get<wbr/></span><span class="pun">.</span><span class="pln">i </span><span class="pun">== </span><span class="lit">42</span><span class="pun">);

}

</span><span class="com">//polymorphic classes:
</span><span class="pun">{
    </span><span class="typ">UniquePtr</span><span class="pun">!</span><span class="typ">Foo </span><span class="pln">foo </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!</span><span class="typ">Bar<wbr/></span><span class="pun">.</span><span class="pln">make</span><span class="pun">(</span><span class="lit">42</span><span class="pun">, </span><span class="lit">3.14</span><span class="pun">);
    </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">foo </span><span class="pun">!= </span><span class="kwd">null</span><span class="pun">);
    </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">foo<wbr/></span><span class="pun">.</span><span class="pln">get<wbr/></span><span class="pun">.</span><span class="pln">i </span><span class="pun">== </span><span class="lit">42</span><span class="pun">);

    </span><span class="com">//dynamic cast:
    </span><span class="pun">{
        </span><span class="typ">UniquePtr</span><span class="pun">!</span><span class="typ">Bar </span><span class="pln">bar </span><span class="pun">= <a href="../../autoptr/unique_ptr/dynCastMove.html"></span><span class="pln">dynCastMove</span></a><span class="pun">!</span><span class="typ">Bar</span><span class="pun">(</span><span class="pln">foo</span><span class="pun">);
        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">foo </span><span class="pun">== </span><span class="kwd">null</span><span class="pun">);
        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">bar </span><span class="pun">!= </span><span class="kwd">null</span><span class="pun">);

        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">bar<wbr/></span><span class="pun">.</span><span class="pln">get<wbr/></span><span class="pun">.</span><span class="pln">i </span><span class="pun">== </span><span class="lit">42</span><span class="pun">);
        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">bar<wbr/></span><span class="pun">.</span><span class="pln">get<wbr/></span><span class="pun">.</span><span class="pln">d </span><span class="pun">== </span><span class="lit">3.14</span><span class="pun">);
    }

}

</span><span class="com">//dynamic array
</span><span class="pun">{
    </span><span class="kwd">import </span><span class="pln">std<wbr/></span><span class="pun">.</span><span class="pln">algorithm </span><span class="pun">: </span><span class="pln">all</span><span class="pun">;

    {
        </span><span class="kwd">auto </span><span class="pln">arr </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="typ">long</span><span class="pun">[])<wbr/>.</span><span class="pln">make</span><span class="pun">(</span><span class="lit">10</span><span class="pun">, -</span><span class="lit">1</span><span class="pun">);

        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">arr<wbr/></span><span class="pun">.</span><span class="pln">length </span><span class="pun">== </span><span class="lit">10</span><span class="pun">);
        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">arr<wbr/></span><span class="pun">.</span><span class="pln">get<wbr/></span><span class="pun">.</span><span class="pln">all</span><span class="pun">!(</span><span class="pln">x </span><span class="pun">=&gt; </span><span class="pln">x </span><span class="pun">== -</span><span class="lit">1</span><span class="pun">));
    }

    {
        </span><span class="kwd">auto </span><span class="pln">arr </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="typ">long</span><span class="pun">[])<wbr/>.</span><span class="pln">make</span><span class="pun">(</span><span class="lit">8</span><span class="pun">);
        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">arr<wbr/></span><span class="pun">.</span><span class="pln">length </span><span class="pun">== </span><span class="lit">8</span><span class="pun">);
        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">arr<wbr/></span><span class="pun">.</span><span class="pln">get<wbr/></span><span class="pun">.</span><span class="pln">all</span><span class="pun">!(</span><span class="pln">x </span><span class="pun">=&gt; </span><span class="pln">x </span><span class="pun">== </span><span class="typ">long<wbr/></span><span class="pun">.</span><span class="pln">init</span><span class="pun">));
    }
}

</span><span class="com">//static array
</span><span class="pun">{
    </span><span class="kwd">import </span><span class="pln">std<wbr/></span><span class="pun">.</span><span class="pln">algorithm </span><span class="pun">: </span><span class="pln">all</span><span class="pun">;

    {
        </span><span class="kwd">auto </span><span class="pln">arr </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="typ">long</span><span class="pun">[</span><span class="lit">4</span><span class="pun">])<wbr/>.</span><span class="pln">make</span><span class="pun">(-</span><span class="lit">1</span><span class="pun">);
        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">arr<wbr/></span><span class="pun">.</span><span class="pln">get</span><span class="pun">[]<wbr/>.</span><span class="pln">all</span><span class="pun">!(</span><span class="pln">x </span><span class="pun">=&gt; </span><span class="pln">x </span><span class="pun">== -</span><span class="lit">1</span><span class="pun">));

    }

    {
        </span><span class="typ">long</span><span class="pun">[</span><span class="lit">4</span><span class="pun">] </span><span class="pln">tmp </span><span class="pun">= [</span><span class="lit">0</span><span class="pun">, </span><span class="lit">1</span><span class="pun">, </span><span class="lit">2</span><span class="pun">, </span><span class="lit">3</span><span class="pun">];
        </span><span class="kwd">auto </span><span class="pln">arr </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="typ">long</span><span class="pun">[</span><span class="lit">4</span><span class="pun">])<wbr/>.</span><span class="pln">make</span><span class="pun">(</span><span class="pln">tmp</span><span class="pun">);
        </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">arr<wbr/></span><span class="pun">.</span><span class="pln">get</span><span class="pun">[] == </span><span class="pln">tmp</span><span class="pun">[]);
    }
}

</span></code></pre>
</section>
<section><h2>Example</h2>

<pre class="code"><code class="lang-d"><span class="com">//make UniquePtr object
</span><span class="kwd">static struct </span><span class="typ">Foo</span><span class="pun">{
    </span><span class="typ">int </span><span class="pln">i</span><span class="pun">;

    </span><span class="kwd">this</span><span class="pun">(</span><span class="typ">int </span><span class="pln">i</span><span class="pun">)</span><span class="kwd">pure nothrow @safe @nogc</span><span class="pun">{
        </span><span class="kwd">this<wbr/></span><span class="pun">.</span><span class="pln">i </span><span class="pun">= </span><span class="pln">i</span><span class="pun">;
    }
}

{
    </span><span class="kwd">auto </span><span class="pln">foo </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!</span><span class="typ">Foo<wbr/></span><span class="pun">.</span><span class="pln">make</span><span class="pun">(</span><span class="lit">42</span><span class="pun">);
    </span><span class="kwd">auto </span><span class="pln">foo2 </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!</span><span class="typ">Foo<wbr/></span><span class="pun">.</span><span class="pln">make</span><span class="pun">!</span><span class="typ">Mallocator</span><span class="pun">(</span><span class="lit">42</span><span class="pun">);  </span><span class="com">//explicit stateless allocator
</span><span class="pun">}

{
    </span><span class="kwd">auto </span><span class="pln">arr </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="typ">long</span><span class="pun">[])<wbr/>.</span><span class="pln">make</span><span class="pun">(</span><span class="lit">10</span><span class="pun">); </span><span class="com">//dynamic array with length 10
    </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">arr<wbr/></span><span class="pun">.</span><span class="pln">length </span><span class="pun">== </span><span class="lit">10</span><span class="pun">);
}
</span></code></pre>
</section>
<section><h2>Example</h2>

<pre class="code"><code class="lang-d"><span class="com">//alloc UniquePtr object
</span><span class="kwd">import </span><span class="pln">std<wbr/></span><span class="pun">.</span><span class="pln">experimental<wbr/></span><span class="pun">.</span><span class="pln">allocator </span><span class="pun">: </span><span class="pln">make</span><span class="pun">, </span><span class="pln">dispose</span><span class="pun">, </span><span class="pln">allocatorObject</span><span class="pun">;

</span><span class="kwd">auto </span><span class="pln">allocator </span><span class="pun">= </span><span class="pln">allocatorObject</span><span class="pun">(</span><span class="typ">Mallocator<wbr/></span><span class="pun">.</span><span class="pln">instance</span><span class="pun">);

{
    </span><span class="kwd">auto </span><span class="pln">x </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="typ">long</span><span class="pun">)<wbr/>.</span><span class="pln">alloc</span><span class="pun">(</span><span class="pln">allocator</span><span class="pun">, </span><span class="lit">42</span><span class="pun">);
}

{
    </span><span class="kwd">auto </span><span class="pln">arr </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="typ">long</span><span class="pun">[])<wbr/>.</span><span class="pln">alloc</span><span class="pun">(</span><span class="pln">allocator</span><span class="pun">, </span><span class="lit">10</span><span class="pun">); </span><span class="com">//dynamic array with length 10
    </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">arr<wbr/></span><span class="pun">.</span><span class="pln">length </span><span class="pun">== </span><span class="lit">10</span><span class="pun">);
}

</span></code></pre>
</section>
<section><h2>Example</h2>

<pre class="code"><code class="lang-d"><span class="pun">{
    </span><span class="kwd">auto </span><span class="pln">x </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="kwd">shared </span><span class="typ">long</span><span class="pun">)<wbr/>.</span><span class="pln">make</span><span class="pun">(</span><span class="lit">123</span><span class="pun">);

    </span><span class="kwd">shared </span><span class="pln">s </span><span class="pun">= <a href="../../autoptr/unique_ptr/share.html"></span><span class="pln">share</span></a><span class="pun">(</span><span class="pln">x<wbr/></span><span class="pun">.</span><span class="pln">move</span><span class="pun">);
    </span><span class="kwd">assert</span><span class="pun">(</span><span class="pln">x </span><span class="pun">== </span><span class="kwd">null</span><span class="pun">);

    </span><span class="kwd">auto </span><span class="pln">y </span><span class="pun">= </span><span class="pln">s<wbr/></span><span class="pun">.</span><span class="pln">exchange</span><span class="pun">(</span><span class="kwd">null</span><span class="pun">);
    </span><span class="kwd">assert</span><span class="pun">(*</span><span class="pln">y </span><span class="pun">== </span><span class="lit">123</span><span class="pun">);
}

{
    </span><span class="kwd">auto </span><span class="pln">x </span><span class="pun">= </span><span class="typ">UniquePtr</span><span class="pun">!(</span><span class="typ">long</span><span class="pun">)<wbr/>.</span><span class="pln">make</span><span class="pun">(</span><span class="lit">123</span><span class="pun">);

    </span><span class="com">///error `shared UniquePtr` need shared `ControlType` and shared `ElementType`.
    //shared s = share(x);

</span><span class="pun">}
</span></code></pre>
</section>

			<footer>
				<table class="license-info">
					<tr>
						<th>Authors</th>
						<td>
							<p>github.com/submada/basic_string, Adam Búš
</p>

						</td>
					</tr>
					<tr>
						<th>Copyright</th>
						<td>
							
						</td>
					</tr>
					<tr>
						<th>License</th>
						<td>
							<p>www.boost.org/LICENSE_1_0.txt, Boost License 1.0.
</p>

						</td>
					</tr>
				</table>
				<p class="faint">Generated using the DDOX documentation generator</p>
			</footer>
		</div>
	</body>
</html>